package io.renren.modules.api.config;

import io.renren.common.utils.PropertiesUtil;
import io.renren.modules.api.interceptor.AuthorizationInterceptor;
import io.renren.modules.api.resolver.LoginUserHandlerMethodArgumentResolver;
import io.renren.modules.oss.constant.UploadConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * MVC配置
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-04-20 22:30
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Autowired
    private AuthorizationInterceptor authorizationInterceptor;
    @Autowired
    private LoginUserHandlerMethodArgumentResolver loginUserHandlerMethodArgumentResolver;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authorizationInterceptor).addPathPatterns("/api/**");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(loginUserHandlerMethodArgumentResolver);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String path = PropertiesUtil.getProperties_1(UploadConstant.ROOT_UPLOAD_FILE_PATH, UploadConstant.RESOURCES_URL);

        String zipPath = PropertiesUtil.getProperties_1(UploadConstant.ROOT_UPLOAD_FILE_PATH, UploadConstant.ZIP_FILE_URL);
//        String path = "D:\\qgb\\images";
        System.out.println("***********文件路径：" + path);
        //此处设置为查看静态文件的位置，上传的文件根据这个设置来查看上传的图片视频或者音频
        registry.addResourceHandler("/"+ UploadConstant.ZIP_FILE +"/**").addResourceLocations("file:"+zipPath+"/");
        registry.addResourceHandler("/"+ UploadConstant.IMAGES_ROUTE +"/**").addResourceLocations("file:"+path+"/");
        super.addResourceHandlers(registry);
    }
}